/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.server.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.daikit.commons.shared.utils.DkIdGenerator;
import com.daikit.daikit4gxtsample.shared.bean.CityData;
import com.daikit.daikit4gxtsample.shared.bean.CountryDetailData;
import com.daikit.daikit4gxtsample.shared.bean.LocaleData;
import com.daikit.daikit4gxtsample.shared.bean.ProductData;
import com.daikit.daikit4gxtsample.shared.bean.ProductDetailData;
import com.daikit.daikit4gxtsample.shared.bean.ProductExtendingSpecialData;
import com.daikit.daikit4gxtsample.shared.bean.ProductManager;
import com.daikit.daikit4gxtsample.shared.bean.ProductSpecialData;
import com.daikit.daikit4gxtsample.shared.bean.ProductVerySpecialData;
import com.daikit.daikit4gxtsample.shared.bean.ShopData;
import com.daikit.daikit4gxtsample.shared.bean.StockDetailData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierAvailabilityData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierDetailData;
import com.daikit.daikit4gxtsample.shared.bean.WarehouseData;


@SuppressWarnings("javadoc")
public class DataStore
{

	public static final List<ProductData> PRODUCTS = new ArrayList<ProductData>();
	public static final List<SupplierData> SUPPLIERS = new ArrayList<SupplierData>();
	public static final List<WarehouseData> WAREHOUSES = new ArrayList<WarehouseData>();
	public static final List<LocaleData> LOCALES = new ArrayList<LocaleData>();
	public static final List<CountryDetailData> COUNTRYDETAILS = new ArrayList<CountryDetailData>();
	public static final List<CityData> CITIES = new ArrayList<CityData>();
	public static final List<ShopData> SHOPS = new ArrayList<ShopData>();

	private static int productCounter = 1;
	private static int supplierDetailCounter = 1;
	private static int stockDetailCounter = 1;
	private static int detailCounter = 1;
	private static int supplierCounter = 1;
	private static int warehouseCounter = 1;
	private static int productManagerCounter = 1;
	private static int localePropertyCounter = 1;

	static
	{
		LOCALES.add(new LocaleData(Locale.FRANCE.getISO3Language(), Locale.FRANCE.getDisplayLanguage()));
		LOCALES.add(new LocaleData(Locale.ENGLISH.getISO3Language(), Locale.ENGLISH.getDisplayLanguage()));
		LOCALES.add(new LocaleData(Locale.ITALIAN.getISO3Language(), Locale.ITALIAN.getDisplayLanguage()));
		LOCALES.add(new LocaleData(Locale.GERMANY.getISO3Language(), Locale.GERMANY.getDisplayLanguage()));
		LOCALES.add(new LocaleData(Locale.US.getISO3Language(), Locale.US.getDisplayLanguage()));

		for (int i = 0; i < 80; i++)
		{
			final CountryDetailData countryDetail = new CountryDetailData();
			countryDetail.setCountryName("country name #" + i);
			countryDetail.setName("name #" + i);
			countryDetail.setSalesForecast(i);
			COUNTRYDETAILS.add(countryDetail);
		}

		for (int i = 0; i < 180; i++)
		{
			final CityData city = new CityData();
			city.setCountryName("country name #" + i);
			city.setName("name #" + i);
			city.setPostalCode("postal code #" + i);
			CITIES.add(city);
		}

		for (int i = 0; i < 500; i++)
		{
			final ShopData shop = new ShopData();
			shop.setName("shop #" + i);
			shop.setCity(CITIES.get(Double.valueOf(Math.floor(Integer.valueOf(i).doubleValue() / 4d)).intValue()));
			SHOPS.add(shop);
		}

		for (int i = 0; i < 20; i++)
		{
			final SupplierData supplier = new SupplierData();
			supplier.setName("Supplier #" + supplierCounter++);
			SUPPLIERS.add(supplier);
		}

		for (int i = 0; i < 500; i++)
		{
			final WarehouseData warehouse = new WarehouseData();
			warehouse.setName("Warehouse #" + warehouseCounter++);
			WAREHOUSES.add(warehouse);
		}

		for (int i = 0; i < 150; i++)
		{
			final ProductData product;
			if (i % 3 == 0)
			{
				product = new ProductSpecialData();
				((ProductSpecialData) product).setSpecial("special property");
			}
			else if (i % 3 == 1)
			{
				product = new ProductExtendingSpecialData();
				((ProductSpecialData) product).setSpecial("special extending property");
			}
			else
			{
				product = new ProductVerySpecialData();
				((ProductVerySpecialData) product).setVerySpecial("very special property");
			}
			product.setId(DkIdGenerator.generateRandomId(7));
			final int x = productCounter++;
			product.setName("Product #" + x);
			product.setProperty1("property #" + x + " #1");
			product.setProperty2("property #" + x + " #2");
			product.setProperty3("property #" + x + " #3");
			product.setProperty4("property #" + x + " #4");
			product.setPropertyBoolean(Boolean.valueOf(i % 2 == 0));
			product.setPropertyRichText("<span class=\"test\"><b>é<b><br/></span>");
			for (int j = 0; j < 15; j++)
			{
				final SupplierDetailData supplierDetail = new SupplierDetailData();
				supplierDetail.setId(DkIdGenerator.generateRandomId(7));
				supplierDetail.setName("Supplier Detail #" + supplierDetailCounter++);
				supplierDetail.setSupplier(SUPPLIERS.get(j));
				supplierDetail.setWarehouse(WAREHOUSES.get(j));
				final SupplierAvailabilityData supplierAvailabilityData = new SupplierAvailabilityData();
				supplierAvailabilityData.setStartDate(new Date());
				supplierAvailabilityData.setEndDate(i % 2 == 0 ? new Date() : null);
				supplierDetail.setAvailability(supplierAvailabilityData);
				product.getSupplierDetails().add(supplierDetail);
				for (int k = 0; k < 10; k++)
				{
					final StockDetailData stockDetail = new StockDetailData();
					stockDetail.setId(DkIdGenerator.generateRandomId(7));
					stockDetail.setName("Stock Detail #" + stockDetailCounter++);
					supplierDetail.getStockDetails().add(stockDetail);
				}
			}
			final ProductDetailData productDetail = new ProductDetailData();
			productDetail.setDetail("detail #" + detailCounter++);
			productDetail.setId(DkIdGenerator.generateRandomId(7));
			product.setProductDetail(productDetail);
			final ProductManager productManager = new ProductManager();
			productManager.setName("Product Manager #" + productManagerCounter++);
			product.setProductManager(productManager);

			for (int k = 0; k < Math.random() * 5; k++)
			{
				product.getComments().add("Comment #" + k);
			}

			for (final LocaleData locale : LOCALES)
			{
				if (Math.random() > 0.4d)
				{
					product.getLocalizedProperty().put(locale, "locale property value #" + localePropertyCounter++);
				}
			}

			for (int k = 0; k < 5; k++)
			{
				product.getShops().add(SHOPS.get(k));
			}

			PRODUCTS.add(product);
		}
	}

	public static final ProductData getProductById(final String id)
	{
		ProductData ret = null;
		for (final ProductData product : PRODUCTS)
		{
			if (id.equals(product.getId()))
			{
				ret = product;
				break;
			}
		}
		return ret;
	}

	public static final SupplierDetailData getSupplierDetailById(final String id)
	{
		SupplierDetailData ret = null;
		for (final ProductData product : PRODUCTS)
		{
			for (final SupplierDetailData supplierDetail : product.getSupplierDetails())
			{
				if (supplierDetail.getId().equals(id))
				{
					ret = supplierDetail;
				}
			}
		}
		return ret;
	}

	public static final SupplierAvailabilityData getSupplierAvailabilityBySupplierDetailId(final String supplierDetailsId)
	{
		final SupplierDetailData supplierDetail = getSupplierDetailById(supplierDetailsId);
		return supplierDetail == null ? null : supplierDetail.getAvailability();
	}

	public static StockDetailData getStockDetailById(final String id)
	{
		StockDetailData ret = null;
		for (final ProductData product : PRODUCTS)
		{
			for (final SupplierDetailData supplierDetail : product.getSupplierDetails())
			{
				for (final StockDetailData stockDetail : supplierDetail.getStockDetails())
				{
					if (stockDetail.getId().equals(id))
					{
						ret = stockDetail;
					}
				}
			}
		}
		return ret;
	}

	public static final void removeById(final String id)
	{
		ProductData toRemove = null;
		for (final ProductData product : PRODUCTS)
		{
			if (id.equals(product.getId()))
			{
				toRemove = product;
				break;
			}
		}
		if (toRemove != null)
		{
			PRODUCTS.remove(toRemove);
		}
	}

	public static final void replace(final ProductData oldOne, final ProductData newOne)
	{
		int index = -1;
		for (int i = 0; i < PRODUCTS.size(); i++)
		{
			if (PRODUCTS.get(i).getId().equals(oldOne.getId()))
			{
				index = i;
				break;
			}
		}
		if (index != -1)
		{
			PRODUCTS.remove(index);
			PRODUCTS.add(index, newOne);
		}
	}

}
